var _page = $('.page');
var target_id = $(_page).find("input[name='user_id']").val();
// 分享
$(_page).on('click', '.js-handle', function() {
    bottlecms.actions([
        [{
            text: '分享链接',
            onClick: function(modal, e) {
                share_fun();
            }
        }, {
            text: '分享相亲卡',
            onClick: function(modal, e) {
                single_card_fun();
            }
        }, {
            text: '举报',
            onClick: function(modal, e) {
                report_fun(target_id);
            }
        }, {
            text: '拉黑',
            onClick: function(modal, e) {
                blacklist_fun(target_id);
            }
        }],
        [{
            text: '取消',
            bg: 'danger'
        }]
    ]);
});
//头部滚动透明
var js_avatar_height = $(window).width() * 0.8;
$(_page).find(".page-content").scroll(function() {
    var top = $(this).scrollTop();
    var num = top / js_avatar_height;
    if (num > 1) {
        $(_page).find(".head-nav").removeClass('single_show_header')
    } else {
        $(_page).find(".head-nav").addClass('single_show_header')
    }
}).trigger("scroll");

// 照片预览
$('.album .photos .item').on('click', function() {
    var photos = [];
    $('.album .photos').find('img').each(function(index, el) {
        var src = $(this).attr('max-src');
        var w = ($(this)[0].naturalWidth || 100);
        var h = ($(this)[0].naturalHeight || 100);
        photos.push({
            src:src,
            w: (w*10),
            h:(h*10)
        });
    });

    var index = $(document).find('.album .photos .item').index(this) || 0;
    bottlecms.photoBrowser({
        photos: photos,
    }).open(index)
});

// 心动
$(_page).on('click', '.js-like', function() {
    var _this = this;
    var act = $(this).hasClass('on') ? 2 : 1;
    like_fun(target_id, act, function() {
        $(_this).toggleClass('on');
    })
})
// 聊天
$(_page).on('click', '.js-chat', function() {
    bottlecms.goto("/m/chat/" + $("#userId").val());
})
// 送礼物
$(_page).on('click', '.js-gift', function() {
    gift_fun(target_id)
})
// 打招呼
$(_page).on('click', '.js-greet', function() {
    greet_fun(target_id)
})
// 查看联系方式
$(_page).on('click', '.js-apply_contact', function() {
    var item = $(this).attr('item');
    apply_contact_fun(target_id, null, null, item)
})
// 评价






var data = [
        'https://demo.bottlecms.com/bc-application/Mobile/Assets/Mobile/img/single_share_card.png',
        'https://demo.bottlecms.com/crossdomain?type=image/jpg&url=https://demo.bottlecms.com/uploads/single/photo/6.jpeg?x-oss-process=image/resize,m_fill,h_200,w_200',
        'https://demo.bottlecms.com/crossdomain?type=image/jpg&url=https://demo.bottlecms.com/uploads/single/photo/6.jpeg?x-oss-process=image/resize,m_fill,h_200,w_200'
    ],
    base64 = [];
var position = [[0,220,216,1000,184],[0,60,1005,1000,840]]; //position[0]为left值，position[1]为top值（图片相对位置），依次为图片1-图片3、文字1-文字2
var size = [[600,160,10],[900,160,10]];//size[0]为width值，size[1]为height值（图片相对大小），依次为图片1-图片3
var text = [
    '湿内隧台可',
    '女 . 23岁 . 186cm . 射手座',
    ''
];

function single_card_fun(){
    $("#qrcode").qrcode({
        render: "canvas",
        width: 300,
        height:300,
        text: "https://demo.bottlecms.com/m/single/6?eid=d0cfd338f125fcbce2bf10a83494a73a"
    });
    draw(function() {
        bottlecms.modal({content:'<img src="'+base64[0]+'" width="300" height="450" /><div style="color#F80;position:absolute; bottom:10px; right:10px;font-size:12px;">按住我转发</div>'});
    })
}
function draw(fn) {
    var c = document.createElement('canvas'),
        ctx = c.getContext('2d'),
        len = data.length;
    c.width = size[0][0];
    c.height = size[1][0];
    ctx.rect(0, 0, c.width, c.height);

    function drawing(n) {
        if (n < len) {
            var img = new Image;
            img.src = data[n];
            if(n==0){
                img.onload=function(){
                    ctx.drawImage(img,position[0][n],position[1][n],size[0][n],size[1][n]);
                    ctx.font="32px microsoft yahei";
                    ctx.fillStyle = "rgba(0,0,0,1)";
                    ctx.textAlign = 'center';
                    ctx.fillText(text[0], 300,270);
                    ctx.font="24px microsoft yahei";
                    ctx.fillStyle = "rgba(0,0,0,.6)";
                    ctx.textAlign = 'center';
                    ctx.fillText(text[1],300,310);
                    drawing(n + 1);
                }
            }else if(n==1){
                var c2 = document.createElement('canvas'),
                    ctx2 = c2.getContext('2d');
                c2.width = 160;
                c2.height = 160;
                var img2 = new Image;
                img2.src = data[n];
                img2.onload = function() {
                    ctx2.arc(80, 80, 80, 0, Math.PI * 2, false);
                    ctx2.clip();
                    ctx2.drawImage(img2, 0, 0,160,160);
                    ctx.drawImage(c2, 220,58);
                    drawing(n + 1);
                }
            }else{
                var c3 = document.getElementById("qrcode").getElementsByTagName('canvas')[0];
                ctx.drawImage(c3,180, 550, 240, 240);
                drawing(n + 1);
            }
        } else {
            base64.push(c.toDataURL("image/png", 1));
            fn();
        }
    }
    drawing(0);
}

function bc_currency(currency_type,parse){
    var arr = [];
    arr = {
        'currency_1' : {
            'name' : "现金",
            'unit' : "元",
            'exchange' : ""
        },
        'currency_2' : {
            'name' : "喜糖",
            'unit' : "颗",
            'exchange' : "1"
        },
        'currency_3' : {
            'name' : "",
            'unit' : "",
            'exchange' : ""
        }
    };
    var returnstr = arr;
    if(currency_type){
        returnstr = arr[currency_type];
        if(parse){
            returnstr = returnstr[parse];
        }
    }
    return returnstr;
}